<?php
namespace App\Http\Controllers\Api\Repositories;

use App\Models\Shop\WxShopGoods;
use App\Models\Shop\WxShopGoods as Model;
use App\Models\WxSyncRelation;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class WxShopGoodsRepositories{
    protected $eloquentClass = Model::class;

    /**
     * @param $domain
     * @param $external_id
     * @param $name
     * @param $pic
     * @param array $tag
     * @param $intro
     * @param $vip_price
     * @param $price
     * @param $content
     * @param $buys
     * @param $sku
     * @param $url
     * @param $classify_id
     * @param array $service_id
     * @param int $is_inspiration
     * @param int $state
     * @return bool
     */
    public static function admin_add($domain, $external_id, $name, $pic, array $tag,  $intro, $vip_price, $price,
                                     $content, $buys, $sku, $url, $classify_id,array $service_id, $is_inspiration = 0, $state = 1){
        DB::beginTransaction();
        try {

            $goods_id = WxSyncRelationRepositories::wx_good_id($domain, $external_id);
            if($goods_id){
                return true;
            }

            $goods = new Model();
            $goods->type = $url ? 1 : 0;
            $goods->name = $name ?: '';
            $goods->pic = $pic ?: '';
            $goods->tag = $tag ? json_encode($tag) : '[]';
            $goods->intro = $intro ?: '';
            $goods->vip_price = $vip_price;
            $goods->price = $price;
            $goods->content = $content ?: '';
            $goods->buys = $buys ?: 0;
            $goods->sku = $sku ?: '';
            $goods->url = $url ?: '';
            $goods->classify_id = $classify_id;
            if(_empty_($service_id)){
                $service_id = [];
            }
            if($url){
                if(!in_array(5, $service_id)){
                    $service_id[] = 5;
                }
                $goods->service_id = json_encode($service_id);
            }else{
                $goods->service_id = json_encode($service_id);
            }
            $goods->is_inspiration = 0;
            $goods->state = $state;

            $goods->save();

            $goodsId = $goods->id;

            if($external_id){
                $sync = new WxSyncRelation();
                $sync->domain = $domain;
                $sync->type = 'good';
                $sync->inner_id = $goodsId;
                $sync->external_id = $external_id;
                $sync->status = 1;
                $sync->save();
            }

            DB::commit();
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            _logger_(__file__, __line__, $e->getMessage());
            return false;
        }
    }


    /**
     * @param $dtk_id
     * @param $tk_platform_id
     * @param $name
     * @param $pic
     * @param $tag 数组
     * @param $intro
     * @param $vip_price
     * @param $price
     * @param $content
     * @param $buys
     * @param $sku
     * @param $url
     * @param $classify_id
     * @param $service_id 数组
     * @return int|mixed
     */
    public static function taoke_add($dtk_id, $tk_platform_id, $name, $pic, $tag, $intro, $vip_price, $price, $content, $buys, $sku, $url, $classify_id, $service_id, $url_word){
        DB::beginTransaction();
        try {
            if($dtk_id == -1 || strlen($dtk_id) <= 4){
                $dtk_id = null;
            }

            if($dtk_id){
                $id = WxShopGoods::where('dtk_id', $dtk_id)->value('id');
                if($id){
                    return $id;
                }
            }

            $id = WxShopGoods::where('tk_platform_id', $tk_platform_id)->value('id');
            if($id){
                return $id;
            }

            $goods = new Model();
            $goods->dtk_id = $dtk_id;
            $goods->tk_platform_id = $tk_platform_id;
            $goods->type = 1;
            $goods->name = $name ?: '';
            $goods->pic = $pic ?: '';
            $goods->tag = $tag ? json_encode($tag) : '[]';
            $goods->intro = $intro ?: '';
            $goods->vip_price = $vip_price;
            $goods->price = $price;
            $goods->content = $content ?: '';
            $goods->buys = $buys ?: 0;
            $goods->sku = $sku ?: '';
            $goods->url = $url ?: '';
            $goods->classify_id = $classify_id;
            if(_empty_($service_id)){
                $service_id = [];
            }
            if(!_empty_($url_word)){
                $goods->url_word = $url_word;
            }
            if($url){
                if(!in_array(5, $service_id)){
                    $service_id[] = 5;
                }
                $goods->service_id = json_encode($service_id);
            }else{
                $goods->service_id = json_encode($service_id);
            }
            $goods->is_inspiration = 0;
            $goods->state = 1;

            $goods->save();

            $goodsId = $goods->id;


            DB::commit();
            return $goodsId;
        } catch (\Exception $e) {
            DB::rollBack();
            _logger_(__file__, __line__, $e->getMessage());
            return 0;
        }
    }


    /**
     * 搜索
     * @param $keyword
     * @return mixed
     */
    public static function searchGoods($keyword, $uid = 0, $limit = 10)
    {
        $data = (new Model())
            ->where('state', 1)
            ->where(function ($query) use ($keyword) {
                $query->orWhere('name   ', 'like', '%' . $keyword . '%')
                    ->orWhere('intro', 'like', '%' . $keyword . '%')
                    ->orWhere('id', $keyword);
            })
            ->simplePaginate($limit);
        if($data){
            return $data;
        }
        return null;
    }

}
